def isMaxArr(arr):
    for i in range(len(arr) - 1):
        for j in range(i + 1, len(arr)):
            if arr[i] < arr[j]:
                return False
    return True

class SqLine:
    width = 4
    sq = []
    def __init__(self, args):
        width = args
        for i in range(width * width):
            self.sq.append(i + 1)
    def getLineSum(self):
        sum = 0
        for v in self.sq:
            sum += v
        lineSum = int(sum / self.width)
        return lineSum
    def prt(self):
        print("+----+----+----+----+")
        print("| " + self.form(0) + " | " + self.form(1) + " | " + self.form(2) + " | " + self.form(3) + " |")
        print("+----+----+----+----+")
        print("| " + self.form(4) + " | " + self.form(5) + " | " + self.form(6) + " | " + self.form(7) + " |")
        print("+----+----+----+----+")
        print("| " + self.form(8) + " | " + self.form(9) + " | " + self.form(10) + " | " + self.form(11) + " |")
        print("+----+----+----+----+")
        print("| " + self.form(12) + " | " + self.form(13) + " | " + self.form(14) + " | " + self.form(15) + " |")
        print("+----+----+----+----+")
    def form(self, idx): 
        val = self.sq[idx]
        if val < 10:
            return " " + str(val)
        return "" + str(val)
    def isSum34(self, i1, i2, i3, i4):
        if self.sq[i1] + self.sq[i2] + self.sq[i3] + self.sq[i4] == 34:
            return True
        return False
    def swapValByIdx(self, idx1, idx2): 
        tmp = self.sq[idx1]
        self.sq[idx1] = self.sq[idx2]
        self.sq[idx2] = tmp
    def findChangeIdx(self):
        for i in range(len(self.sq) - 1, 0, -1):
            if self.sq[i] > self.sq[i - 1]:
                return i - 1
        return -1
    def findNextIdxByIdx(self, fromIdx):
        fromNum = self.sq[fromIdx]
        miniNum = 17
        idx = 15
        for i in range(fromIdx, len(self.sq)):
            if self.sq[i] > fromNum:
                if self.sq[i] < miniNum:
                    miniNum = self.sq[i]
                    idx = i
        return idx
    def reSortFromIdx(self, idx):
        if idx < len(self.sq) - 2:
            tmp = self.sq[idx + 1:]
            tmp.sort()
            self.sq = self.sq[:idx + 1] + tmp

    def next(self):
        if not isMaxArr(self.sq):
            idx = self.findChangeIdx()
            nextNumIdx = self.findNextIdxByIdx(idx)
            self.swapValByIdx(idx, nextNumIdx)
            self.reSortFromIdx(idx)
    def sort(self):
        while not isMaxArr(self.sq) :
            r1 = self.isSum34(0, 1, 2, 3)
            if not r1:
                tmp = self.sq[4:]
                tmp.sort(reverse=True)
                self.sq = self.sq[:4] + tmp
                self.next()
                continue
            xf1 = self.isSum34(0, 1, 4, 5)
            if not xf1 :
                tmp = self.sq[6:]
                tmp.sort(reverse=True)
                self.sq = self.sq[:6] + tmp
                self.next()
                continue
            xf2 = self.isSum34(1, 2, 5, 6)
            if not xf2 :
                tmp = self.sq[7:]
                tmp.sort(reverse=True)
                self.sq = self.sq[:7] + tmp
                self.next()
                continue
            r2  = self.isSum34(4, 5, 6, 7)
            xf3 = self.isSum34(2, 3, 6, 7)
            if (not r2) or (not xf3) :
                tmp = self.sq[8:]
                tmp.sort(reverse=True)
                self.sq = self.sq[:8] + tmp
                self.next()
                continue
            xf4 = self.isSum34(4, 5, 8, 9)
            if not xf4 :
                tmp = self.sq[10:]
                tmp.sort(reverse=True)
                self.sq = self.sq[:10] + tmp
                self.next()
                continue

            xf5 = self.isSum34(5, 6, 9, 10)
            if not xf5 :
                tmp = self.sq[11:]
                tmp.sort(reverse=True)
                self.sq = self.sq[:11] + tmp
                self.next()
                continue
            r3  = self.isSum34(8, 9, 10, 11)
            xf6 = self.isSum34(6, 7, 10, 11)
            if (not r3) or (not xf6) :
                tmp = self.sq[12:]
                tmp.sort(reverse=True)
                self.sq = self.sq[:12] + tmp
                self.next()
                continue
            c1 = self.isSum34(0, 4, 8, 12)
            if not c1 :
                tmp = self.sq[13:]
                tmp.sort(reverse=True)
                self.sq = self.sq[:13] + tmp
                self.next()
                continue
            q2 = self.isSum34(3, 6, 9, 12)
            if not q2 :
                tmp = self.sq[13:]
                tmp.sort(reverse=True)
                self.sq = self.sq[:13] + tmp
                self.next()
                continue
            c2 = self.isSum34(1, 5, 9, 13)
            if not c2 :
                tmp = self.sq[14:]
                tmp.sort(reverse=True)
                self.sq = self.sq[:14] + tmp
                self.next()
                continue
            xf7 = self.isSum34(8, 9, 12, 13)
            if not xf7 :
                tmp = self.sq[14:]
                tmp.sort(reverse=True)
                self.sq = self.sq[:14] + tmp
                self.next()
                continue
            xf8 = self.isSum34(9, 10, 13, 14)
            if not xf8 :
                tmp = self.sq[15:]
                tmp.sort(reverse=True)
                self.sq = self.sq[:15] + tmp
                self.next()
                continue
            c3 = self.isSum34(2, 6, 10, 14)
            if not c3 :
                tmp = self.sq[6:]
                tmp.sort(reverse=True)
                self.sq = self.sq[:6] + tmp
                self.next()
                continue
            q1 = self.isSum34(0, 5, 10, 15)
            if not q1 :
                self.next()
                continue
            if r1 and r2 and r3 and c1 and c2 and c3 and q1 and q2 :
                self.prt()
                print("")
            self.next()

sl = SqLine(4)
sl.sort()

